package com.mall.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.bean.Cart;
import com.mall.bean.Goods;
import com.mall.bean.Order;
import com.mall.bean.User;
import com.mall.service.ICartService;
import com.mall.service.IOrderService;
import com.mall.service.IPersonalService;

@Controller
public class OrderController {

	@Resource
	private IOrderService orderService;

	@Resource
	private ICartService cartService;

	@Resource
	private IPersonalService personalService;


	//**待发货**立即购买成功,已付款待发货
	@RequestMapping("purchase")
	@ResponseBody
	public Map<String, Object> purchase(Order order, HttpSession session, Model model, Cart cart) {

		User user = (User) session.getAttribute("user");

		Map<String, Object> resMap = new HashMap<String, Object>();
		order.setUser_id(user.getUser_id());
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdata = ft.format(time);
		order.setOrder_date(nowdata);
		//取消购买后将物品添加到待付款区域
		String status = "待发货";
		order.setOrder_status(status);
		System.out.println("order:" + order);
		//查询购物车中购买的数
		User purchase = cartService.myCart(user);
		Iterator<Goods> it = purchase.getGoods().iterator();
		while (it.hasNext()) {
			Goods goods = it.next();
			//未支付加入订单表中
			order.setGoods_url1(goods.getGoods_url1());
			order.setGoods_name(goods.getGoods_name());
			order.setGoods_id(goods.getGoods_id());
			order.setNumber(goods.getGoods_number());
			order.setPrice(goods.getGoods_D_price());
			System.out.println("第一次循环" + order);
			orderService.addOrder(order);
			//删除购物车中商品
			cart.setUser_id(goods.getGoods_id());
			cartService.delCartGoods(cart);
		}

		return resMap;

	}

	//**待付款**取消购买，未付款进入待付款区域
	@RequestMapping("cancelPurchase")
	@ResponseBody
	public Map<String, Object> cancelPurchase(Order order, HttpSession session, Model model, Cart cart) {

		User user = (User) session.getAttribute("user");

		Map<String, Object> resMap = new HashMap<String, Object>();
		order.setUser_id(user.getUser_id());
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdata = ft.format(time);
		order.setOrder_date(nowdata);
		//取消购买后将物品添加到待付款区域
		String status = "待付款";
		order.setOrder_status(status);
		System.out.println("order:" + order);
		//查询购物车中购买的数
		User purchase = cartService.myCart(user);
		Iterator<Goods> it = purchase.getGoods().iterator();
		while (it.hasNext()) {
			Goods goods = it.next();
			//未支付加入订单表中
			order.setGoods_url1(goods.getGoods_url1());
			order.setGoods_name(goods.getGoods_name());
			order.setGoods_id(goods.getGoods_id());
			order.setNumber(goods.getGoods_number());
			order.setPrice(goods.getGoods_D_price());
			System.out.println("第一次循环" + order);
			orderService.addOrder(order);
			//删除购物车中商品
			cart.setUser_id(goods.getGoods_id());
			cartService.delCartGoods(cart);
		}

		return resMap;

	}

	//展示订单
	@RequestMapping("person/order")
	public String showAllOrder(Cart cart, HttpSession session, Model model, Order order) {
		User user = (User) session.getAttribute("user");
		//展示个人头像以及信息
		User user1 = personalService.findPerMsg(user);
		//所有订单
		User allorder = orderService.showAllOrder(order);
		//待付款
		User unplay = orderService.unplayGoods(order);
		//待发货
		User unsend = orderService.unsendGoods(order);
		//待收货
		User unreceive = orderService.unreceiveGoods(order);
		//待评价
		User uncommnet = orderService.uncommnetGoods(order);
		model.addAttribute("user", user1);
		model.addAttribute("allorder", allorder);
		model.addAttribute("unplay", unplay);
		model.addAttribute("unsend", unsend);
		model.addAttribute("unreceive", unreceive);
		model.addAttribute("uncommnet", uncommnet);
		System.out.println("全部订单：" + allorder);
		return "person/order";
	}


	//**提醒发货**
	@RequestMapping("remindSend")
	public String remindSend(@Param("order_id") Integer order_id, HttpSession session, Order order, Model model) {

		order.setOrder_status("待收货");
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdata = ft.format(time);
		order.setOrder_date(nowdata);
		order.setOrder_id(order_id);
		orderService.updateOrder(order);
		User user = (User) session.getAttribute("user");
		//展示个人头像以及信息
		User user1 = personalService.findPerMsg(user);
		//所有订单
		User allorder = orderService.showAllOrder(order);
		//待付款
		User unplay = orderService.unplayGoods(order);
		//待发货
		User unsend = orderService.unsendGoods(order);
		//待收货
		User unreceive = orderService.unreceiveGoods(order);
		//待评价
		User uncommnet = orderService.uncommnetGoods(order);
		model.addAttribute("user", user1);
		model.addAttribute("allorder", allorder);
		model.addAttribute("unplay", unplay);
		model.addAttribute("unsend", unsend);
		model.addAttribute("unreceive", unreceive);
		model.addAttribute("uncommnet", uncommnet);
		System.out.println("全部订单：" + allorder);

		return "person/order";

	}

	//**一键支付**
	@RequestMapping("remindPay")
	public String remindPay(@Param("order_id") Integer order_id, HttpSession session, Order order, Model model) {
		order.setOrder_status("待发货");
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdata = ft.format(time);
		order.setOrder_date(nowdata);
		order.setOrder_id(order_id);
		orderService.updateOrder(order);
		User user = (User) session.getAttribute("user");
		//展示个人头像以及信息
		User user1 = personalService.findPerMsg(user);
		//所有订单
		User allorder = orderService.showAllOrder(order);
		//待付款
		User unplay = orderService.unplayGoods(order);
		//待发货
		User unsend = orderService.unsendGoods(order);
		//待收货
		User unreceive = orderService.unreceiveGoods(order);
		//待评价
		User uncommnet = orderService.uncommnetGoods(order);
		model.addAttribute("user", user1);
		model.addAttribute("allorder", allorder);
		model.addAttribute("unplay", unplay);
		model.addAttribute("unsend", unsend);
		model.addAttribute("unreceive", unreceive);
		model.addAttribute("uncommnet", uncommnet);
		System.out.println("全部订单：" + allorder);

		return "person/order";

	}

	//**立即收货**
	@RequestMapping("remindReceive")
	public String remindReceive(@Param("order_id") Integer order_id, HttpSession session, Order order, Model model) {

		order.setOrder_status("待评价");
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdata = ft.format(time);
		order.setOrder_date(nowdata);
		order.setOrder_id(order_id);
		orderService.updateOrder(order);
		User user = (User) session.getAttribute("user");
		//展示个人头像以及信息
		User user1 = personalService.findPerMsg(user);
		//所有订单
		User allorder = orderService.showAllOrder(order);
		//待付款
		User unplay = orderService.unplayGoods(order);
		//待发货
		User unsend = orderService.unsendGoods(order);
		//待收货
		User unreceive = orderService.unreceiveGoods(order);
		//待评价
		User uncommnet = orderService.uncommnetGoods(order);
		model.addAttribute("user", user1);
		model.addAttribute("allorder", allorder);
		model.addAttribute("unplay", unplay);
		model.addAttribute("unsend", unsend);
		model.addAttribute("unreceive", unreceive);
		model.addAttribute("uncommnet", uncommnet);
		System.out.println("全部订单：" + allorder);

		return "person/order";
	}

	//**立即评价**
	@RequestMapping("remindComment")
	public String remindComment(@Param("order_id") Integer order_id, HttpSession session, Order order, Model model) {
		order.setOrder_status("已完成");
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowdata = ft.format(time);
		order.setOrder_date(nowdata);
		order.setOrder_id(order_id);
		orderService.updateOrder(order);
		User user = (User) session.getAttribute("user");
		//展示个人头像以及信息
		User user1 = personalService.findPerMsg(user);
		//所有订单
		User allorder = orderService.showAllOrder(order);
		//待付款
		User unplay = orderService.unplayGoods(order);
		//待发货
		User unsend = orderService.unsendGoods(order);
		//待收货
		User unreceive = orderService.unreceiveGoods(order);
		//待评价
		User uncommnet = orderService.uncommnetGoods(order);
		model.addAttribute("user", user1);
		model.addAttribute("allorder", allorder);
		model.addAttribute("unplay", unplay);
		model.addAttribute("unsend", unsend);
		model.addAttribute("unreceive", unreceive);
		model.addAttribute("uncommnet", uncommnet);
		System.out.println("全部订单：" + allorder);

		return "person/order";
	}

	//已完成删除订单

	//申请退货
	@RequestMapping("refund")
	public String refund() {

		return "";
	}

	//退货完成钱到账
	@RequestMapping("refundComplete")
	public String refundComplete() {

		return "";
	}


}
